package cn.hn.yh;

import cn.hn.yh.vo.user.User;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/index")
public class LoginIndex {
    @Autowired
    private Environment environment;
    @Autowired
    private ObjectMapper objectMapper;

    @PostMapping(value = "/login")
    public String login(@RequestBody User user) {
//        Subject subject = SecurityUtils.getSubject();
//        subject.login(new UsernamePasswordToken(user.getUserename(), user.getPassword()));

        String username = environment.getProperty("user.username");
        String password = environment.getProperty("user.password");
        System.out.println(user.getUsername());
        System.out.println(user.getPassword());
        System.out.println(username);
        System.out.println(password);
        Map<String, Object> map = new HashMap<>();
        String str = "";
        if (user.getUsername() != null && !username.equals("")) {
            if (user.getUsername().equals(username) && user.getPassword().equals(password)) {
                map.put("code", 200);
                map.put("msg", "登录成功");
                map.put("token","thisisatoken");
                System.out.println("有人访问后端服务器成功");
            } else {
                map.put("code", 403);
                map.put("msg", "用户名或密码错误");
                System.out.println("有人访问后端服务器失败1");
            }
        } else {
            map.put("code", 401);
            map.put("msg", "未输入有效账户");
            System.out.println("有人访问后端服务器失败2");
        }
        try {
            str = objectMapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return str;
    }
}
